﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using WXEntity;

namespace TestFrm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void 执行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // 获取配置信息
            ServerEntity serverEntity = new ServerEntity(textBox1.Text.Trim());
            //加载程序集(dll文件地址)，使用Assembly类   
            string dllPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, serverEntity.Assembly + ".dll");
            Assembly assembly = Assembly.LoadFile(dllPath);

            ////获取类型，参数（名称空间+类）   
            Type type = assembly.GetType(serverEntity.NameSpaceClassName);

            ////创建该对象的实例，object类型，参数（名称空间+类）   
            object instance = assembly.CreateInstance(serverEntity.NameSpaceClassName);

            ////设置被调用方法中的参数类型，Type[]类型；如有多个参数可以追加多个   
            Type[] params_type = new Type[1];
            params_type[0] = typeof(RequestEntity);
            ////设置被调用方法中的参数值；如有多个参数可以追加多个   
            RequestEntity[] params_obj = new RequestEntity[1];
            RequestEntity requestEntity = Newtonsoft.Json.JsonConvert.DeserializeObject<RequestEntity>(richTextBox1.Text.Trim());
            Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
            JObject jObject = requestEntity.RequestParam as JObject;
            string jsonDict = Newtonsoft.Json.JsonConvert.SerializeObject(jObject);
            keyValuePairs = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonDict);
            requestEntity.RequestParam = keyValuePairs;
            params_obj[0] = requestEntity;

            ////执行Show_Str方法   
            object value = type.GetMethod(serverEntity.Method, params_type).Invoke(instance, params_obj);

            ResultEntity resultEntity = value as ResultEntity;
            string msg = Newtonsoft.Json.JsonConvert.SerializeObject(resultEntity);
            MessageFrm messageFrm = new MessageFrm();
            messageFrm.SetMsg(msg);
            messageFrm.Show();
        }
    }
}
